<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <!--不用设置actions与methods    -->
    <form id="form" enctype="multipart/form-data">
        <input type="text" name="username" >
        <button id="button" type="button">发送</button>
    </form>
    <script>
        let button = document.querySelector('#button');
        //  当用户选择完文件并确认后触发函数
        button.onclick = function (){
            // 获取form元素
            let form = document.querySelector('#form');
            //  将form转换为formData表单对象
            let formData = new FormData(form);
            // 创建ajax对象
            let xhr = new XMLHttpRequest();
            // formData不能用于get请求,因为要放在send()中,send(formData)
            xhr.open('post','http://localhost:8080')
            // 跨域请求携带Cookie
            xhr.withCredentials = true;
            // 发送ajax对象
            xhr.send(formData)
            // 客户端接受到服务器响应触发
            xhr.onload = function (){
                // 打印Cookie
                console.log(xhr.responseText)
            }
        }
    </script>
</body>
</html>